package com.yc154.web;

import com.yc154.entity.User;
import com.yc154.web.feign.UserAction;
import javax.servlet.http.HttpServletRequest;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
// 动态配置刷新
@RefreshScope
public class IndexDo {
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        // 获取请求参数 例如: URL?a=1&b=2
        // request.getQueryString();
        return "index: " + request.getServerPort();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("fromUser")
    public String user(){
        // 远程访问 user 获取数据, 返回给浏览器
        String res = restTemplate.getForObject(
                "http://user/user", String.class);
        return res;
    }

    @Autowired
    UserAction userAction;

    @RequestMapping("fromUserByFeign")
    public String fromUserByFeign(){
        // 远程访问 user 获取数据, 返回给浏览器
        return userAction.user();
    }

    @RequestMapping("add")
    public String add(int a, int b){
        return "result = " + userAction.add(a, b);
    }

    @RequestMapping("hello")
    public String hello(String name){
        return userAction.hello(name);
    }

    @RequestMapping("emp1")
    public User emp1(){
        User user = new User();
        user.setId(1);
        user.setName("emp1");
        user.setAge(10);
        user.setSex("男");
        return userAction.emp1(user);
    }

    @RequestMapping("emp2")
    public User emp2(){
        User user = new User();
        user.setId(1);
        user.setName("emp2");
        user.setAge(10);
        user.setSex("男");
        return userAction.emp2(user);
    }

    @RequestMapping("emp11")
    public User emp3(){
        User user = new User();
        user.setId(1);
        user.setName("emp11");
        user.setAge(10);
        user.setSex("男");
        return userAction.emp11(user);
    }

    @RequestMapping("emp22")
    public User emp4(){
        User user = new User();
        user.setId(1);
        user.setName("emp22");
        user.setAge(10);
        user.setSex("男");
        return userAction.emp22(user);
    }

    @Value("${upload.path}")
    String uploadPath;

    @RequestMapping("uploadPath")
    String getUploadPath(){
        return uploadPath;
    }


    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("simple_queue")
    public String test一对一消费() {
        String queueName = "simple.queue"; // 队列名称
        String message = "hello, simple.queue"; // 要发送的消息
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(queueName, message + " " + i);
        }
        return "一对10消费 => 生产OK";
    }

    @GetMapping("send/{ex}/{route}/{msg}")
    public String send(@PathVariable("ex") String ex,
                       @PathVariable("route") String route,
                       @PathVariable("msg") String msg){
        // 广播消息, 路由为空
        if(route.equals("fanout")){
            route = "";
        }
        rabbitTemplate.convertAndSend(ex, route, msg);
        return String.format("发送消息 '%s' 到交换机 '%s', 路由 '%s'", msg,  ex, route);
    }

}
